﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XStudio.XCRF.Data.Business;
using XStudio.XCRF.Data.Business.Permissions;
using XStudio.XCRF.Data.Entities;
using XStudio.XCRF.Data.Entities.CureInfo;
using Telerik.Web.Mvc;
using System.Text;
using XStudio.XCRF.Data.Enums;

namespace XStudio.XCRF.Web.Controllers
{
    /// <summary>
    /// 治疗信息控制器
    /// </summary>
    [DefaultPermission(PermissionType.Allow)]
    public partial class CureInfoController : BaseController
    {
        #region 治疗信息
        public ActionResult CureInfoAdd(Guid crfID)
        {
            CRF crf = DataContext.CRFs.Include(c => c.Patient).Find(crfID);
            CureInfo cureInfo = new CureInfo
            {
                CRFID = crfID,
                CRF = crf
            };
            return View(cureInfo);
        }

        [HttpPost]
        public ActionResult CureInfoAdd(CureInfo cureInfo)
        {
            try
            {
                DataContext.CureInfoes.Add(cureInfo);
                DataContext.SaveChanges();
                return Json(new { ID = cureInfo.ID });
            }
            catch
            {
                return Json(null);
            }
        }

        //public ActionResult CureInfoEdit(Guid cureInfoID)
        //{
        //    CureInfo cureInfo = ((IQueryable<CureInfo>)DataContext.CureInfoes).Find(cureInfoID);
        //    return View(cureInfo);
        //}

        public ActionResult CureInfoEdit(Guid cureInfoID)
        {
            CureInfo cureInfo = ((IQueryable<CureInfo>)DataContext.CureInfoes
                .Include(cure => cure.PrimarySyms)
                .Include(cure => cure.SubSyms)
                ).Find(cureInfoID);
            return View(cureInfo);
        }

        [HttpPost]
        public ActionResult CureInfoEdit(CureInfo cureInfo)
        {
            try
            {
                CureInfo cureInfo_db = DataContext.CureInfoes
                    .Include(cure => cure.PrimarySyms)
                    .Include(cure => cure.SubSyms)
                    .Find(cureInfo.ID);
                cureInfo_db.Complaint = cureInfo.Complaint;
                cureInfo_db.Symptom = cureInfo.Symptom;
                cureInfo_db.VitalSign = cureInfo.VitalSign;
                cureInfo_db.Reaction = cureInfo.Reaction;
                cureInfo_db.Trans = cureInfo.Trans;
                cureInfo_db.Advice = cureInfo.Advice;
                cureInfo_db.KPS = cureInfo.KPS;
                cureInfo_db.ECOG = cureInfo.ECOG;
                cureInfo_db.QOL = cureInfo.QOL;

                if (cureInfo.TongueColor == (byte)TongueColorTypes.Unknow)
                    cureInfo_db.TongueColorOther = cureInfo.TongueColorOther;
                else
                    cureInfo_db.TongueColorOther = "";
                if (cureInfo.TongueShape == (byte)TongueShapeTypes.Unknow)
                    cureInfo_db.TongueShapeOther = cureInfo.TongueShapeOther;
                else
                    cureInfo_db.TongueShapeOther = "";
                if (cureInfo.TongueStatus == (byte)TongueStatusTypes.Unknow)
                    cureInfo_db.TongueStatusOther = cureInfo.TongueStatusOther;
                else
                    cureInfo_db.TongueStatusOther = "";
                if (cureInfo.FurCharacter == (byte)FurCharacterTypes.Unknow)
                    cureInfo_db.FurCharacterOther = cureInfo.FurCharacterOther;
                else
                    cureInfo_db.FurCharacterOther = "";
                if (cureInfo.FurColor == (byte)FurColorTypes.Unknow)
                    cureInfo_db.FurColorOther = cureInfo.FurColorOther;
                else
                    cureInfo_db.FurColorOther = "";
                if (cureInfo.DeepPulseClass == (byte)DeepPulseClassTypes.Unknow)
                    cureInfo_db.DeepPulseClassOther = cureInfo.DeepPulseClassOther;
                else
                    cureInfo_db.DeepPulseClassOther = "";
                if (cureInfo.ExcessPulseClass == (byte)ExcessPulseClassTypes.Unknow)
                    cureInfo_db.ExcessPulseClassOther = cureInfo.ExcessPulseClassOther;
                else
                    cureInfo_db.ExcessPulseClassOther = "";
                if (cureInfo.FeeblePulseClass == (byte)FeeblePulseClassTypes.Unknow)
                    cureInfo_db.FeeblePulseClassOther = cureInfo.FeeblePulseClassOther;
                else
                    cureInfo_db.FeeblePulseClassOther = "";
                if (cureInfo.FloatingPulseClass == (byte)FloatingPulseClassTypes.Unknow)
                    cureInfo_db.FloatingPulseClassOther = cureInfo.FloatingPulseClassOther;
                else
                    cureInfo_db.FloatingPulseClassOther = "";
                if (cureInfo.RapidPulseClass == (byte)RapidPulseClassTypes.Unknow)
                    cureInfo_db.RapidPulseClassOther = cureInfo.RapidPulseClassOther;
                else
                    cureInfo_db.RapidPulseClassOther = "";
                if (cureInfo.SlowPulseClass == (byte)SlowPulseClassTypes.Unknow)
                    cureInfo_db.SlowPulseClassOther = cureInfo.SlowPulseClassOther;
                else
                    cureInfo_db.SlowPulseClassOther = "";

                UpdateDiseases(cureInfo_db.PrimarySyms, cureInfo.PrimarySyms);
                UpdateDiseases(cureInfo_db.SubSyms, cureInfo.SubSyms);

                //cureInfo_db.Pulse = cureInfo.Pulse;
                //cureInfo_db.Tongue = cureInfo.Tongue;
                cureInfo_db.DeepPulseClass = cureInfo.DeepPulseClass;
                cureInfo_db.ExcessPulseClass = cureInfo.ExcessPulseClass;
                cureInfo_db.FeeblePulseClass = cureInfo.FeeblePulseClass;
                cureInfo_db.FloatingPulseClass = cureInfo.FloatingPulseClass;
                cureInfo_db.RapidPulseClass = cureInfo.RapidPulseClass;
                cureInfo_db.SlowPulseClass = cureInfo.SlowPulseClass;
                cureInfo_db.FurCharacter = cureInfo.FurCharacter;
                cureInfo_db.FurColor = cureInfo.FurColor;
                cureInfo_db.TongueColor = cureInfo.TongueColor;
                cureInfo_db.TongueShape = cureInfo.TongueShape;
                cureInfo_db.TongueStatus = cureInfo.TongueStatus;
                cureInfo_db.Herbs = cureInfo.Herbs;

                cureInfo_db.dbt = cureInfo.dbt;
                cureInfo_db.bgms = cureInfo.bgms;
                cureInfo_db.btbt = cureInfo.btbt;
                cureInfo_db.bxcs = cureInfo.bxcs;
                cureInfo_db.bz = cureInfo.bz;
                cureInfo_db.dbgj = cureInfo.dbgj;
                cureInfo_db.dbms = cureInfo.dbms;
                cureInfo_db.ehpl = cureInfo.ehpl;
                cureInfo_db.em = cureInfo.em;
                cureInfo_db.exot = cureInfo.exot;
                cureInfo_db.ft = cureInfo.ft;
                cureInfo_db.ftja = cureInfo.ftja;
                cureInfo_db.fttj = cureInfo.fttj;
                cureInfo_db.ftwz = cureInfo.ftwz;
                cureInfo_db.ftxw = cureInfo.ftxw;
                cureInfo_db.fz = cureInfo.fz;
                cureInfo_db.gmzr = cureInfo.gmzr;
                cureInfo_db.gmzz = cureInfo.gmzz;
                cureInfo_db.kw = cureInfo.kw;
                cureInfo_db.ms = cureInfo.ms;
                cureInfo_db.nl = cureInfo.nl;
                cureInfo_db.ns = cureInfo.ns;
                cureInfo_db.pbbs = cureInfo.pbbs;
                cureInfo_db.spfl = cureInfo.spfl;
                cureInfo_db.sy = cureInfo.sy;
                cureInfo_db.tt = cureInfo.tt;
                cureInfo_db.ty = cureInfo.ty;
                cureInfo_db.wgbh = cureInfo.wgbh;
                cureInfo_db.wxfr = cureInfo.wxfr;
                cureInfo_db.yt = cureInfo.yt;
                cureInfo_db.ytxz = cureInfo.ytxz;
                cureInfo_db.ztcz = cureInfo.ztcz;
                DataContext.SaveChanges();
                return Json(new { ID = cureInfo.ID });
            }
            catch
            {
                return Json(null);
            }
        }

        /// <summary>
        /// 更新主/次证
        /// </summary>
        /// <param name="oldDiseases">数据库值</param>
        /// <param name="newDiseases">新值</param>
        private void UpdateDiseases(ICollection<Disease> oldDiseases, IEnumerable<Disease> newDiseases)
        {
            IEnumerable<Guid> oldIDs = oldDiseases == null ? new List<Guid>() : oldDiseases.Select(d => d.ID);   //数据库Id集合
            IEnumerable<Guid> newIDs = newDiseases == null ? new List<Guid>() : newDiseases.Select(d => d.ID);   //新值Id集合
            IEnumerable<Guid> crossIDs = oldIDs.Intersect(newIDs);      //交集Id集合
            IEnumerable<Guid> deletedIDs = oldIDs.Except(crossIDs);     //将要删除的Id集合
            IEnumerable<Guid> addedIDs = newIDs.Except(crossIDs);       //将要添加的Id集合
            IEnumerable<Disease> deletedDiseases = DataContext.Diseases.Where(d => deletedIDs.Contains(d.ID));
            IEnumerable<Disease> addedDiseases = DataContext.Diseases.Where(d => addedIDs.Contains(d.ID));
            oldDiseases.RemoveMany(deletedDiseases);
            oldDiseases.AddMany(addedDiseases);
        }
        /*public ActionResult CureInfoDelete(Guid cureInfoID)
        {
            CureInfo cureInfo = DataContext.CureInfoes.Include(l => l.CRF).Find(cureInfoID);
            DataContext.CureInfoes.Remove(cureInfo);
            DataContext.SaveChanges();
            return View(cureInfo);
            //throw new NotImplementedException();
        }*/

        /*public ActionResult CureInfoDetail(Guid cureInfoID)
        {
            CureInfo cureInfo = ((IQueryable<CureInfo>)DataContext.CureInfoes).Find(cureInfoID);
            return View(cureInfo);
        }*/
        public ActionResult CureInfoDetail(Guid cureInfoID)
        {
            CureInfo cureInfo = DataContext.CureInfoes.Include(l => l.CRF).Find(cureInfoID);
            return View(cureInfo);
        }
        /*public ActionResult CureInfoDetail(Guid cureInfoID)
        {
            CureInfo cureInfo = DataContext.CureInfoes.Include(c => c.CRFID).Find(cureInfoID);
            DbEntityEntry<CureInfo> entry = DataContext.Entry<CureInfo>(cureInfo);
            entry.Collection<Symptom>(c => c.Symptoms).Query().GetMasterRecords().Load();
            entry.Collection<Syndrome>(c => c.Syndromes).Query().GetMasterRecords().Load();
            entry.Collection<CureHerb>(c => c.Herbs).Query().GetMasterRecords().Load();
            entry.Collection<Inject>(c => c.Injects).Query().GetMasterRecords().Load();
            entry.Collection<CureChemo>(c => c.Chemos).Query().GetMasterRecords().Load();
            entry.Collection<CureRadio>(c => c.Radios).Query().GetMasterRecords().Load();
            entry.Collection<CureOtherCure>(c => c.OtherCures).Query().GetMasterRecords().Load();
            return View(cureInfo);
        }*/
        #endregion 治疗信息

        #region 中成药、中药注射液
        /// <summary>
        /// 处理显示中成药、中药注射液部分页请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult InjectList(Guid cureInfoID)
        {
            List<Inject> injs = DataContext.Injects
                .Find(f => f.CureInfoID == cureInfoID).ToList();
            return PartialView(injs);
        }

        /// <summary>
        /// 处理显示中成药、中药注射液编辑模式请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult InjectGridView(Guid cureInfoID)
        {
            CureInfo cureInfo = DataContext.CureInfoes.Find(cureInfoID);
            return PartialView(cureInfo);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _InjectList(Guid cureInfoID)
        {
            IEnumerable<Inject> dtos = DataContext.Injects.Find(c => c.CureInfoID == cureInfoID).ToList()
                .Select(c => new Inject
                {
                    ID = c.ID,
                    Name = c.Name,
                    StartTime = c.StartTime,
                    EndTime = c.EndTime,
                    Remark = c.Remark
                });
            return View(new GridModel(dtos));
        }

        [GridAction]
        [HttpPost]
        public ActionResult _InjectAdd(Inject inj, Guid cureInfoID)
        {
            DataContext.Injects.Add(inj);
            DataContext.SaveChanges();
            return _InjectList(cureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _InjectEdit(Inject inj)
        {
            Inject inj_db = DataContext.Injects.Find(inj.ID);
            inj_db.Name = inj.Name;
            inj_db.StartTime = inj.StartTime;
            inj_db.EndTime = inj.EndTime;
            inj_db.Remark = inj.Remark;
            DataContext.SaveChanges();
            return _InjectList((Guid)inj_db.CureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _InjectDelete(Inject inj)
        {
            Inject inj_db = ((IQueryable<Inject>)DataContext.Injects).Find(inj.ID);
            inj.CureInfoID = inj_db.CureInfoID;
            DataContext.Injects.Remove(inj_db);
            DataContext.SaveChanges();
            return _InjectList((Guid)inj.CureInfoID);
        }
        #endregion 中成药、中药注射液

        #region 化疗
        /// <summary>
        /// 处理显示化疗部分页请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult CureChemoList(Guid cureInfoID)
        {
            List<CureChemo> chemoes = DataContext.CureChemoes
                .Find(f => f.CureInfoID == cureInfoID).ToList();
            return PartialView(chemoes);
        }

        /// <summary>
        /// 处理显示化疗编辑模式请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult CureChemoGridView(Guid cureInfoID)
        {
            CureInfo cureInfo = DataContext.CureInfoes.Find(cureInfoID);
            return PartialView(cureInfo);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureChemoList(Guid cureInfoID)
        {
            IEnumerable<CureChemo> dtos = DataContext.CureChemoes.Find(c => c.CureInfoID == cureInfoID).ToList()
                .Select(c => new CureChemo
                {
                    ID = c.ID,
                    Scheme = c.Scheme,
                    StartTime = c.StartTime,
                    EndTime = c.EndTime,
                    Remark = c.Remark
                });
            return View(new GridModel(dtos));
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureChemoAdd(CureChemo chemo, Guid cureInfoID)
        {
            DataContext.CureChemoes.Add(chemo);
            DataContext.SaveChanges();
            return _CureChemoList(cureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureChemoEdit(CureChemo chemo)
        {
            CureChemo chemo_db = DataContext.CureChemoes.Find(chemo.ID);
            chemo_db.Scheme = chemo.Scheme;
            chemo_db.StartTime = chemo.StartTime;
            chemo_db.EndTime = chemo.EndTime;
            chemo_db.Remark = chemo.Remark;
            DataContext.SaveChanges();
            return _CureChemoList((Guid)chemo_db.CureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureChemoDelete(CureChemo chemo)
        {
            CureChemo chemo_db = ((IQueryable<CureChemo>)DataContext.CureChemoes).Find(chemo.ID);
            chemo.CureInfoID = chemo_db.CureInfoID;
            DataContext.CureChemoes.Remove(chemo_db);
            DataContext.SaveChanges();
            return _CureChemoList((Guid)chemo.CureInfoID);
        }
        #endregion 化疗

        #region 放疗
        /// <summary>
        /// 处理显示放疗部分页请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult CureRadioList(Guid cureInfoID)
        {
            List<CureRadio> radioes = DataContext.CureRadioes
                .Find(f => f.CureInfoID == cureInfoID).ToList();
            return PartialView(radioes);
        }

        /// <summary>
        /// 处理显示放疗编辑模式请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult CureRadioGridView(Guid cureInfoID)
        {
            CureInfo cureInfo = DataContext.CureInfoes.Find(cureInfoID);
            return PartialView(cureInfo);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureRadioList(Guid cureInfoID)
        {
            IEnumerable<CureRadio> dtos = DataContext.CureRadioes.Find(c => c.CureInfoID == cureInfoID).ToList()
                .Select(c => new CureRadio
                {
                    ID = c.ID,
                    Place = c.Place,
                    Dose = c.Dose,
                    Times = c.Times,
                    Remark = c.Remark
                });
            return View(new GridModel(dtos));
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureRadioAdd(CureRadio radio, Guid cureInfoID)
        {
            radio.CureInfoID = cureInfoID;
            DataContext.CureRadioes.Add(radio);
            DataContext.SaveChanges();
            return _CureRadioList(cureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureRadioEdit(CureRadio radio)
        {
            CureRadio radio_db = DataContext.CureRadioes.Find(radio.ID);
            radio_db.Place = radio.Place;
            radio_db.Dose = radio.Dose;
            radio_db.Times = radio.Times;
            radio_db.Remark = radio.Remark;
            DataContext.SaveChanges();
            return _CureRadioList((Guid)radio_db.CureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureRadioDelete(CureRadio radio)
        {
            CureRadio radio_db = ((IQueryable<CureRadio>)DataContext.CureRadioes).Find(radio.ID);
            radio.CureInfoID = radio_db.CureInfoID;
            DataContext.CureRadioes.Remove(radio_db);
            DataContext.SaveChanges();
            return _CureRadioList((Guid)radio.CureInfoID);
        }
        #endregion 放疗

        #region 超声刀
        /// <summary>
        /// 处理显示超声刀部分页请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult CureSoundList(Guid cureInfoID)
        {
            List<CureSound> sounds = DataContext.CureSounds
                .Find(f => f.CureInfoID == cureInfoID).ToList();
            return PartialView(sounds);
        }

        /// <summary>
        /// 处理显示超声刀编辑模式请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult CureSoundGridView(Guid cureInfoID)
        {
            CureInfo cureInfo = DataContext.CureInfoes.Find(cureInfoID);
            return PartialView(cureInfo);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureSoundList(Guid cureInfoID)
        {
            IEnumerable<CureSound> dtos = DataContext.CureSounds.Find(c => c.CureInfoID == cureInfoID).ToList()
                .Select(c => new CureSound
                {
                    ID = c.ID,
                    Place = c.Place,
                    Dose = c.Dose,
                    Times = c.Times,
                    Remark = c.Remark
                });
            return View(new GridModel(dtos));
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureSoundAdd(CureSound sound, Guid cureInfoID)
        {
            sound.CureInfoID = cureInfoID;
            DataContext.CureSounds.Add(sound);
            DataContext.SaveChanges();
            return _CureSoundList(cureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureSoundEdit(CureSound sound)
        {
            CureSound sound_db = DataContext.CureSounds.Find(sound.ID);
            sound_db.Place = sound.Place;
            sound_db.Dose = sound.Dose;
            sound_db.Times = sound.Times;
            sound_db.Remark = sound.Remark;
            DataContext.SaveChanges();
            return _CureSoundList((Guid)sound_db.CureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureSoundDelete(CureSound sound)
        {
            CureSound sound_db = ((IQueryable<CureSound>)DataContext.CureSounds).Find(sound.ID);
            sound.CureInfoID = sound_db.CureInfoID;
            DataContext.CureSounds.Remove(sound_db);
            DataContext.SaveChanges();
            return _CureSoundList((Guid)sound.CureInfoID);
        }
        #endregion 超声刀

        #region 伽马刀
        /// <summary>
        /// 处理显示伽马刀部分页请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult CureGammaList(Guid cureInfoID)
        {
            List<CureGamma> gammas = DataContext.CureGammas
                .Find(f => f.CureInfoID == cureInfoID).ToList();
            return PartialView(gammas);
        }

        /// <summary>
        /// 处理显示伽马刀编辑模式请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult CureGammaGridView(Guid cureInfoID)
        {
            CureInfo cureInfo = DataContext.CureInfoes.Find(cureInfoID);
            return PartialView(cureInfo);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureGammaList(Guid cureInfoID)
        {
            IEnumerable<CureGamma> dtos = DataContext.CureGammas.Find(c => c.CureInfoID == cureInfoID).ToList()
                .Select(c => new CureGamma
                {
                    ID = c.ID,
                    Place = c.Place,
                    Dose = c.Dose,
                    Times = c.Times,
                    Remark = c.Remark
                });
            return View(new GridModel(dtos));
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureGammaAdd(CureGamma gamma, Guid cureInfoID)
        {
            gamma.CureInfoID = cureInfoID;
            DataContext.CureGammas.Add(gamma);
            DataContext.SaveChanges();
            return _CureGammaList(cureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureGammaEdit(CureSound gamma)
        {
            CureGamma gamma_db = DataContext.CureGammas.Find(gamma.ID);
            gamma_db.Place = gamma.Place;
            gamma_db.Dose = gamma.Dose;
            gamma_db.Times = gamma.Times;
            gamma_db.Remark = gamma.Remark;
            DataContext.SaveChanges();
            return _CureGammaList((Guid)gamma_db.CureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureGammaDelete(CureGamma gamma)
        {
            CureGamma gamma_db = ((IQueryable<CureGamma>)DataContext.CureGammas).Find(gamma.ID);
            gamma.CureInfoID = gamma_db.CureInfoID;
            DataContext.CureGammas.Remove(gamma_db);
            DataContext.SaveChanges();
            return _CureGammaList((Guid)gamma.CureInfoID);
        }
        #endregion 伽马刀

        #region 其他治疗
        /// <summary>
        /// 处理显示其他治疗部分页请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult CureOtherCureList(Guid cureInfoID)
        {
            List<CureOtherCure> others = DataContext.CureOtherCures
                .Find(f => f.CureInfoID == cureInfoID).ToList();
            return PartialView(others);
        }

        /// <summary>
        /// 处理显示其他治疗编辑模式请求
        /// </summary>
        /// <param name="cureInfoID">基本信息ID</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult CureOtherCureGridView(Guid cureInfoID)
        {
            CureInfo cureInfo = DataContext.CureInfoes.Find(cureInfoID);
            return PartialView(cureInfo);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureOtherCureList(Guid cureInfoID)
        {
            IEnumerable<CureOtherCure> dtos = DataContext.CureOtherCures.Find(c => c.CureInfoID == cureInfoID).ToList()
                .Select(c => new CureOtherCure
                {
                    ID = c.ID,
                    Name = c.Name,
                    StartTime = c.StartTime,
                    EndTime = c.EndTime,
                    Remark = c.Remark
                });
            return View(new GridModel(dtos));
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureOtherCureAdd(CureOtherCure other, Guid cureInfoID)
        {
            DataContext.CureOtherCures.Add(other);
            DataContext.SaveChanges();
            return _CureOtherCureList(cureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureOtherCureEdit(CureOtherCure other)
        {
            CureOtherCure other_db = DataContext.CureOtherCures.Find(other.ID);
            other_db.Name = other.Name;
            other_db.StartTime = other.StartTime;
            other_db.EndTime = other.EndTime;
            other_db.Remark = other.Remark;
            DataContext.SaveChanges();
            return _CureOtherCureList((Guid)other_db.CureInfoID);
        }

        [GridAction]
        [HttpPost]
        public ActionResult _CureOtherCureDelete(CureOtherCure other)
        {
            CureOtherCure other_db = ((IQueryable<CureOtherCure>)DataContext.CureOtherCures).Find(other.ID);
            other.CureInfoID = other_db.CureInfoID;
            DataContext.CureOtherCures.Remove(other_db);
            DataContext.SaveChanges();
            return _CureOtherCureList((Guid)other.CureInfoID);
        }
        #endregion 其他治疗

        #region 辨症

        /// <summary>
        /// 生成草药
        /// </summary>
        /// <param name="diseaseIDs"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GenerateHerb(IEnumerable<Guid> diseaseIDs)
        {
            try
            {
                string strNames;
                if (diseaseIDs == null)
                    strNames = string.Empty;
                else
                {
                    diseaseIDs = diseaseIDs.Distinct();
                    IEnumerable<Disease> diseases = DataContext.Diseases
                        .Include(d => d.Herbs)
                        .Where(d => diseaseIDs.Contains(d.ID)).ToList();
                    IEnumerable<Herb> herbs = diseases.GetHerbs();
                    IEnumerable<string> herbNames = herbs.Select(h => h.Name);
                    strNames = String.Join("; ", herbNames);
                }
                return Json(new { success = true, strNames = strNames });
            }
            catch
            {
                return Json(new { success = false });
            }
        }

        #endregion 辨症

        #region 历史记录
        public ActionResult CureInfoHisGridList(Guid cureInfoID)
        {
            return View(cureInfoID);
        }

        [GridAction]
        public ActionResult _CureInfoHisGridList(Guid cureInfoID)
        {
            List<KeyValuePair<DateTime, string>> t;
            List<KeyValuePair<DateTime, string>> times = DataContext.CureInfoes
                .Where(f => f.ID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            List<KeyValuePair<DateTime, string>> temps = DataContext.CureInfoes
                .Where(f => f.MasterID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;
            temps = DataContext.CureChemoes
                .Where(f => f.CureInfoID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;
            temps = DataContext.CureChemoes
                .Where(f => f.MasterID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;
            temps = DataContext.CureRadioes
                .Where(f => f.CureInfoID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;
            temps = DataContext.CureRadioes
                .Where(f => f.MasterID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;
            temps = DataContext.CureSounds
                .Where(f => f.CureInfoID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;
            temps = DataContext.CureSounds
                .Where(f => f.MasterID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;
            temps = DataContext.CureGammas
                .Where(f => f.CureInfoID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;
            temps = DataContext.CureGammas
                .Where(f => f.MasterID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;
            temps = DataContext.CureOtherCures
                .Where(f => f.CureInfoID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;
            temps = DataContext.CureOtherCures
                .Where(f => f.MasterID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .Select(a => new KeyValuePair<DateTime, string>(a.LastModifyTime, a.LastModifier.RealName))
                .ToList();
            t = times.Union(temps).ToList();
            times = t;

            t = times.OrderByDescending(f => f.Key.Date).ThenByDescending(f => f.Key.TimeOfDay).ToList();
            times = t;
            List<CureInfo> cureInfoes = new List<CureInfo>();
            CureInfo cureInfo, cinfo;
            CureInfo record = DataContext.CureInfoes.Find<CureInfo>(cureInfoID);
            int flag = 0;
            foreach (KeyValuePair<DateTime, string> kp in times)
            {
                if (flag < 2)
                {
                    flag++;
                    continue;
                }
                List<CureInfo> c1 = DataContext.CureInfoes
                    .Where(f => f.ID == cureInfoID)
                    .AsEnumerable()
                    .Distinct()
                    .ToList();
                List<CureInfo> c2 = DataContext.CureInfoes
                    .Where(f => f.MasterID == cureInfoID)
                    .AsEnumerable()
                    .Distinct()
                    .ToList();
                List<CureInfo> c3 = c1.Union(c2).OrderByDescending(f => f.LastModifyTime).ToList();
                cinfo = (from h in c3
                         where h.LastModifyTime <= kp.Key
                         orderby h.LastModifyTime descending
                         select h).First();
                cureInfo = new CureInfo();
                cureInfo.ID = record.ID;
                cureInfo.Time = cinfo.Time;
                cureInfo.CreateTime = cinfo.CreateTime;
                cureInfo.Creater = new Member { RealName = cinfo.Creater.RealName };
                cureInfo.LastModifyTime = kp.Key;
                cureInfo.LastModifier = new Member { RealName = kp.Value };
                cureInfoes.Add(cureInfo);
            }
            return View(new GridModel(cureInfoes));
        }

        public ActionResult _CureInfoDetail(Guid cureInfoID, string tt)
        {
            CureInfo cureInfo;
            DateTime lastModifyTime = Convert.ToDateTime(tt);
            CureInfo record = DataContext.CureInfoes
                .Include(b => b.CRF)
                .Include(b => b.CRF.Patient)
                .Find<CureInfo>(cureInfoID);
            List<CureInfo> c1 = DataContext.CureInfoes
                    .Where(f => f.ID == cureInfoID)
                    .AsEnumerable()
                    .Distinct()
                    .ToList();
            List<CureInfo> c2 = DataContext.CureInfoes
                .Where(f => f.MasterID == cureInfoID)
                .AsEnumerable()
                .Distinct()
                .ToList();
            List<CureInfo> c3 = c1.Union(c2).OrderByDescending(f => f.LastModifyTime).ToList();
            cureInfo = (from h in c3
                        where h.LastModifyTime <= lastModifyTime
                        orderby h.LastModifyTime descending
                        select h).First();
            cureInfo.LastModifyTime = lastModifyTime;
            cureInfo.CRF = record.CRF;
            cureInfo.CRFID = record.CRFID;
            cureInfo.CRF.Patient.Gender = record.CRF.Patient.Gender;
            cureInfo.CRF.Patient.Birthday = record.CRF.Patient.Birthday;
            cureInfo.Archives = null;
            cureInfo.IsDeleted = false;

            IEnumerable<CureChemo> cc = (from f in record.Chemos
                                         where f.LastModifyTime <= lastModifyTime
                                         orderby f.LastModifyTime descending
                                         group f by f.CureInfoID into g
                                         select g.First());
            cureInfo.Chemos = cc.ToList();
            IEnumerable<CureRadio> cr = (from f in record.Radios
                                         where f.LastModifyTime <= lastModifyTime
                                         orderby f.LastModifyTime descending
                                         group f by f.CureInfoID into g
                                         select g.First());
            cureInfo.Radios = cr.ToList();
            IEnumerable<CureSound> cs = (from f in record.Sounds
                                         where f.LastModifyTime <= lastModifyTime
                                         orderby f.LastModifyTime descending
                                         group f by f.CureInfoID into g
                                         select g.First());
            cureInfo.Sounds = cs.ToList();
            IEnumerable<CureGamma> cg = (from f in record.Gammas
                                         where f.LastModifyTime <= lastModifyTime
                                         orderby f.LastModifyTime descending
                                         group f by f.CureInfoID into g
                                         select g.First());
            cureInfo.Gammas = cg.ToList();
            IEnumerable<CureOtherCure> coc = (from f in record.OtherCures
                                              where f.LastModifyTime <= lastModifyTime
                                              orderby f.LastModifyTime descending
                                              group f by f.CureInfoID into g
                                              select g.First());
            cureInfo.OtherCures = coc.ToList();

            return View(cureInfo);
        }
        #endregion
    }
}